package global

import (
	"gitee.com/Frank098/devops-app-mgt/utils"
	"time"
)

// ServerSettingS 服务器配置
type ServerSettingS struct {
	RunMode      string //调试级别
	HttpPort     string //地址
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

// DatabaseSettingS 数据库配置
type DatabaseSettingS struct {
	DBType       string
	UserName     string
	Password     string
	Host         string
	DBName       string
	Charset      string
	ParseTime    bool
	MaxIdleConns int
	MaxOpenConns int
}

// RedisSettingS redis配置
type RedisSettingS struct {
	Address  string
	Password string
}

type KubeConfigS struct {
	Path string
}

// 定义全局变量
var (
	ServerSetting     *ServerSettingS
	DatabaseSetting   *DatabaseSettingS
	RedisSetting      *RedisSettingS
	KubeConfigSetting *KubeConfigS
)

// SetupSetting 读取配置到全局变量
func SetupSetting() error {
	settingS, err := utils.NewSetting()
	if err != nil {
		return err
	}
	err = settingS.ReadSection("Database", &DatabaseSetting)
	if err != nil {
		return err
	}
	err = settingS.ReadSection("Server", &ServerSetting)
	if err != nil {
		return err
	}
	err = settingS.ReadSection("Redis", &RedisSetting)
	if err != nil {
		return err
	}
	err = settingS.ReadSection("KubeConfig", &KubeConfigSetting)
	if err != nil {
		return err
	}

	return nil
}
